package org.thingsboard.mqtt.broker.actors.message;

import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.thingsboard.mqtt.broker.actors.ActorSystemContext;
import org.thingsboard.mqtt.broker.actors.TbActorCtx;
import org.thingsboard.mqtt.broker.actors.TbActorException;
import org.thingsboard.mqtt.broker.actors.client.ClientActorConfiguration;
import org.thingsboard.mqtt.broker.actors.client.messages.mqtt.MqttPublishMsg;
import org.thingsboard.mqtt.broker.actors.client.state.ClientActorState;
import org.thingsboard.mqtt.broker.actors.client.state.DefaultClientActorState;
import org.thingsboard.mqtt.broker.actors.msg.TbActorMsg;
import org.thingsboard.mqtt.broker.actors.service.ContextAwareActor;
import org.thingsboard.mqtt.broker.service.analysis.ClientLogger;
import org.thingsboard.mqtt.broker.service.stats.ClientActorStats;

/**
 * @author fangmingkun
 * @date 2023/8/3 17:01
 * @description
 */
@Slf4j
public class MessageActor extends ContextAwareActor {
    private final ClientLogger clientLogger;
    private final ClientActorConfiguration actorConfiguration;

    private final ClientActorState state;
    private final ClientActorStats clientActorStats;

    public MessageActor(ActorSystemContext systemContext, String clientId, boolean isClientIdGenerated) {
        super(systemContext);
        this.clientLogger = systemContext.getClientActorContext().getClientLogger();
        this.actorConfiguration = systemContext.getClientActorConfiguration();
        this.state = new DefaultClientActorState(clientId, isClientIdGenerated, systemContext.getClientActorContext().getMaxPreConnectQueueSize());
        this.clientActorStats = systemContext.getClientActorContext().getStatsManager().getClientActorStats();
    }

    @Override
    public void init(TbActorCtx ctx) throws TbActorException {
        super.init(ctx);
    }
    @Override
    protected boolean doProcess(TbActorMsg msg) {
        //在这里处理所有publish消息
        MqttPublishMsg publishMsg = (MqttPublishMsg) msg;
        log.info("收到publish消息：{}", publishMsg.getPublishMsg().getByteBuf().toString(CharsetUtil.UTF_8));

        return true;
    }
}
